package com.poisonh.unifiedauth.ui;

import android.app.ProgressDialog;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.ui.svprogresshud.SVProgressHUD;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.RandomUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;
import com.poisonh.unifiedauth.video.CameraHelper;
import com.poisonh.unifiedauth.video.FixedRatioCroppedTextureView;
import com.poisonh.unifiedauth.video.RecordFragment;
import com.poisonh.unifiedauth.video.RecordedFrame;

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacpp.avutil;
import org.bytedeco.javacv.FFmpegFrameFilter;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameFilter;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;

import static java.lang.Thread.State.WAITING;

/**
 * 视频录制
 */
public class FFmpegRecordActivity extends BaseActivity implements TextureView.SurfaceTextureListener, View.OnTouchListener, View.OnClickListener
{
    private static final String LOG_TAG = FFmpegRecordActivity.class.getSimpleName();

    //视频宽高
    private static final int PREFERRED_PREVIEW_WIDTH = 640;
    private static final int PREFERRED_PREVIEW_HEIGHT = 480;

    //录制视频最短、最长时间
    private static final long MIN_VIDEO_LENGTH = 2 * 1000;
    private static final long MAX_VIDEO_LENGTH = 10 * 1000;

    //相机id，前置还是后置
    private int mCameraId;
    private Camera mCamera;
    private FFmpegFrameRecorder mFrameRecorder;
    private VideoRecordThread mVideoRecordThread;
    private AudioRecord mAudioRecord;
    private AudioRecordThread mAudioRecordThread;
    private volatile boolean mRecording = false;
    private File mVideo;
    private LinkedBlockingQueue<RecordedFrame> mRecordedFrameQueue;
    private ConcurrentLinkedQueue<RecordedFrame> mRecycledFrameQueue;
    private int mRecordedFrameCount;
    private int mProcessedFrameCount;
    private long mTotalProcessFrameTime;
    private Stack<RecordFragment> mRecordFragments;

    private int sampleAudioRateInHz = 44100;
    /* The sides of width and height are based on camera orientation.
    That is, the preview size is the size before it is rotated. */
    private int previewWidth = PREFERRED_PREVIEW_WIDTH;
    private int previewHeight = PREFERRED_PREVIEW_HEIGHT;
    // Output video size
    private int videoWidth = 320;
    private int videoHeight = 240;
    private int frameRate = 30;
    private int frameDepth = Frame.DEPTH_UBYTE;
    private int frameChannels = 2;

    private FixedRatioCroppedTextureView mPreview;
    private Button mBtnStartOrPause;
    private TextView mTvShowNumber;
    private String mStrRandomCode;

    private ProgressBar mPbRecordBar;
    private boolean isSaved = true;

    static
    {
        System.loadLibrary("avcodec");
        System.loadLibrary("avdevice");
        System.loadLibrary("avfilter");
        System.loadLibrary("avformat");
        System.loadLibrary("avutil");
        System.loadLibrary("jniavcodec");
        System.loadLibrary("jniavdevice");
        System.loadLibrary("jniavfilter");
        System.loadLibrary("jniavformat");
        System.loadLibrary("jniavutil");
        System.loadLibrary("jniopencv_core");
        System.loadLibrary("jniopencv_imgproc");
        System.loadLibrary("jnipostproc");
        System.loadLibrary("jniswresample");
        System.loadLibrary("jniswscale");
        System.loadLibrary("opencv_core");
        System.loadLibrary("opencv_imgproc");
        System.loadLibrary("postproc");
        System.loadLibrary("swresample");
        System.loadLibrary("swscale");


    }


    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_ffmpeg_record);

        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        setTitleTxt("视频验证");
        mLeftBtn.setOnClickListener(this);
        mRightBtn.setVisibility(View.GONE);

        mPbRecordBar = (ProgressBar) findViewById(R.id.pb_record_bar);
        mPreview = (FixedRatioCroppedTextureView) findViewById(R.id.camera_preview);
        mTvShowNumber = (TextView) findViewById(R.id.tv_show_number);
        mStrRandomCode = RandomUtils.randomSixNum();
        mTvShowNumber.setText(mStrRandomCode);
        mBtnStartOrPause = (Button) findViewById(R.id.btn_start_or_pause);

//        mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
        mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
        // Switch width and height
        mPreview.setPreviewSize(previewHeight, previewWidth);
        mPreview.setCroppedSizeWeight(videoWidth, videoHeight);
        mPreview.setSurfaceTextureListener(this);

        //  mBtnStartOrPause.setOnClickListener(this);
        mBtnStartOrPause.setOnTouchListener(this);
    }


    @Override
    public void onSurfaceTextureAvailable(final SurfaceTexture surface, int width, int height)
    {
        new Thread()
        {
            @Override
            public void run()
            {
                initRecorder();
                startPreview(surface);
                startRecorder();
                startRecording();
            }
        }.start();
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
    {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
    {
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface)
    {
    }

    /**
     * 触摸事件处理
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if (v.getId() == R.id.btn_start_or_pause)
        {
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    if (mRecording)
                    {
                        pauseRecording();
                    } else
                    {
                        resumeRecording();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    pauseRecording();
                    stopRecording();
                    // check video length
                    if (calculateTotalRecordedTime(mRecordFragments) < MIN_VIDEO_LENGTH)
                    {
                        Toast.makeText(this, R.string.video_too_short, Toast.LENGTH_SHORT).show();
                        isSaved = false;
                        mPbRecordBar.setProgress(0);
                        break;
                    }
                    new FinishRecordingTask().execute();
                    break;
            }
        }
        return false;
    }

    private void startPreview(SurfaceTexture surfaceTexture)
    {
        if (mCamera == null)
        {
            return;
        }

        Camera.Parameters parameters = mCamera.getParameters();
        List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
        Camera.Size previewSize = CameraHelper.getOptimalSize(previewSizes,
                PREFERRED_PREVIEW_WIDTH, PREFERRED_PREVIEW_HEIGHT);
        // if changed, reassign values and request layout
        if (previewWidth != previewSize.width || previewHeight != previewSize.height)
        {
            previewWidth = previewSize.width;
            previewHeight = previewSize.height;
            // Switch width and height
            mPreview.setPreviewSize(previewHeight, previewWidth);
            mPreview.requestLayout();
        }
        parameters.setPreviewSize(previewWidth, previewHeight);
//        parameters.setPreviewFormat(ImageFormat.NV21);
        mCamera.setParameters(parameters);

        mCamera.setDisplayOrientation(CameraHelper.getCameraDisplayOrientation(
                this, mCameraId));

        // YCbCr_420_SP (NV21) format
        byte[] bufferByte = new byte[previewWidth * previewHeight * 3 / 2];
        mCamera.addCallbackBuffer(bufferByte);
        mCamera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback()
        {
            @Override
            public void onPreviewFrame(byte[] data, Camera camera)
            {
                // get video data
                if (mRecording)
                {
                    // wait for AudioRecord to init and start
                    if (mAudioRecord == null || mAudioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING)
                    {
                        mRecordFragments.peek().setStartTimestamp(System.currentTimeMillis());
                        return;
                    }

                    // pop the current record fragment when calculate total recorded time
                    RecordFragment curFragment = mRecordFragments.pop();
                    long recordedTime = calculateTotalRecordedTime(mRecordFragments);
                    // push it back after calculation
                    mRecordFragments.push(curFragment);
                    long curRecordedTime = System.currentTimeMillis()
                            - curFragment.getStartTimestamp() + recordedTime;
                    // check if exceeds time limit
                    if (curRecordedTime > MAX_VIDEO_LENGTH)
                    {
                        new FinishRecordingTask().execute();
                        return;
                    }

                    long timestamp = 1000 * curRecordedTime;
                    Frame frame;
                    RecordedFrame recordedFrame = mRecycledFrameQueue.poll();
                    if (recordedFrame != null)
                    {
                        frame = recordedFrame.getFrame();
                        recordedFrame.setTimestamp(timestamp);
                    } else
                    {
                        frame = new Frame(previewWidth, previewHeight, frameDepth, frameChannels);
                        recordedFrame = new RecordedFrame(timestamp, frame);
                    }
                    ((ByteBuffer) frame.image[0].position(0)).put(data);

                    try
                    {
                        mRecordedFrameQueue.put(recordedFrame);
                        mRecordedFrameCount++;
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                mCamera.addCallbackBuffer(data);
            }
        });

        try
        {
            mCamera.setPreviewTexture(surfaceTexture);
        } catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
        mCamera.startPreview();
    }

    private void stopPreview()
    {
        if (mCamera != null)
        {
            mCamera.stopPreview();
            mCamera.setPreviewCallbackWithBuffer(null);
        }
    }

    /**
     * 获得相机
     */
    private void acquireCamera()
    {
        try
        {
            mCamera = Camera.open(mCameraId);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 释放相机
     */
    private void releaseCamera()
    {
        if (mCamera != null)
        {
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }

    /**
     * 初始化资源
     */
    private void initRecorder()
    {
        mRecordedFrameQueue = new LinkedBlockingQueue<>();
        mRecycledFrameQueue = new ConcurrentLinkedQueue<>();
        mRecordFragments = new Stack<>();

        Log.i(LOG_TAG, "init mFrameRecorder");

        String recordedTime = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        mVideo = CameraHelper.getOutputMediaFile(recordedTime, CameraHelper.MEDIA_TYPE_VIDEO);
        Log.i(LOG_TAG, "Output Video: " + mVideo);

        mFrameRecorder = new FFmpegFrameRecorder(mVideo, videoWidth, videoHeight, 1);
        mFrameRecorder.setFormat("mp4");
        mFrameRecorder.setSampleRate(sampleAudioRateInHz);
        mFrameRecorder.setFrameRate(frameRate);
        // Use H264
        mFrameRecorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);

        Log.i(LOG_TAG, "mFrameRecorder initialize success");
    }

    /**
     * 释放录制资源
     */
    private void releaseRecorder()
    {
        mRecordedFrameQueue = null;
        mRecycledFrameQueue = null;
        mRecordFragments = null;

        if (mFrameRecorder != null)
        {
            try
            {
                mFrameRecorder.release();
            } catch (FFmpegFrameRecorder.Exception e)
            {
                e.printStackTrace();
            }
        }
        mFrameRecorder = null;
    }

    /**
     * 开始录制
     */
    private void startRecorder()
    {
        try
        {
            mFrameRecorder.start();
        } catch (FFmpegFrameRecorder.Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 停止录制
     *
     * @param saveFile 是否保存文件
     */
    private void stopRecorder(boolean saveFile)
    {
        if (mFrameRecorder != null)
        {
            try
            {
                mFrameRecorder.stop();
            } catch (FFmpegFrameRecorder.Exception e)
            {
                e.printStackTrace();
            }
            if (!saveFile)
            {
                mVideo.delete();
            }
        }
    }

    /**
     * 开始录制
     */
    private void startRecording()
    {
        mVideoRecordThread = new VideoRecordThread();
        mVideoRecordThread.start();

        mAudioRecordThread = new AudioRecordThread();
        mAudioRecordThread.start();
    }

    /**
     * 停止录制
     */
    private void stopRecording()
    {
        if (mAudioRecordThread != null)
        {
            mAudioRecordThread.stopRunning();
            try
            {
                mAudioRecordThread.join();
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            mAudioRecordThread = null;
        }

        if (mVideoRecordThread != null)
        {
            mVideoRecordThread.stopRunning();
            try
            {
                mVideoRecordThread.join();
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            mVideoRecordThread = null;
        }
    }

    private void resumeRecording()
    {
        if (!mRecording)
        {
            RecordFragment recordFragment = new RecordFragment();
            recordFragment.setStartTimestamp(System.currentTimeMillis());
            mRecordFragments.push(recordFragment);
            mRecording = true;
        }
    }


    /**
     * 计算总共录制时间
     *
     * @param recordFragments
     * @return
     */
    private long calculateTotalRecordedTime(Stack<RecordFragment> recordFragments)
    {
        long recordedTime = 0;
        for (RecordFragment recordFragment : recordFragments)
        {
            recordedTime += recordFragment.getDuration();
        }
        return recordedTime;
    }

    /**
     * 暂停录制
     */
    private void pauseRecording()
    {
        if (mRecording)
        {
            mRecordFragments.peek().setEndTimestamp(System.currentTimeMillis());
            mRecording = false;
        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
        }
    }


    class AudioRecordThread extends Thread
    {

        private boolean isRunning;


        @Override
        public void run()
        {


            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

            // Audio
            int bufferSize = AudioRecord.getMinBufferSize(sampleAudioRateInHz,
                    AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

            mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleAudioRateInHz,
                    AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);

            ShortBuffer audioData = ShortBuffer.allocate(bufferSize);

            Log.d(LOG_TAG, "mAudioRecord.startRecording()");
            mAudioRecord.startRecording();

            isRunning = true;
            /* ffmpeg_audio encoding loop */
            while (isRunning)
            {

                if (mRecording && mFrameRecorder != null)
                {
                    int bufferReadResult = mAudioRecord.read(audioData.array(), 0, audioData.capacity());
                    audioData.limit(bufferReadResult);
                    if (bufferReadResult > 0)
                    {
                        Log.v(LOG_TAG, "bufferReadResult: " + bufferReadResult);
                        try
                        {
                            Message msg = Message.obtain();
                            msg.what = 0;
                            mHandler.sendMessage(msg);

                            mFrameRecorder.recordSamples(audioData);
                        } catch (FFmpegFrameRecorder.Exception e)
                        {
                            Log.v(LOG_TAG, e.getMessage());
                            e.printStackTrace();
                        }
                    }
                }
            }
            Log.v(LOG_TAG, "AudioThread Finished, release mAudioRecord");

            /* encoding finish, release mFrameRecorder */
            if (mAudioRecord != null)
            {
                mAudioRecord.stop();
                mAudioRecord.release();
                mAudioRecord = null;
                Log.v(LOG_TAG, "mAudioRecord released");
            }
        }

        public void stopRunning()
        {
            this.isRunning = false;
        }
    }

    class VideoRecordThread extends Thread
    {

        private boolean isRunning;

        @Override
        public void run()
        {


            List<String> filters = new ArrayList<>();
            // Transpose
            String transpose = null;
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(mCameraId, info);
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
            {
                switch (info.orientation)
                {
                    case 270:
//                        transpose = "transpose=clock_flip"; // Same as preview display
                        transpose = "transpose=cclock"; // Mirrored horizontally as preview display
                        break;
                    case 90:
//                        transpose = "transpose=cclock_flip"; // Same as preview display
                        transpose = "transpose=clock"; // Mirrored horizontally as preview display
                        break;
                }
            } else
            {
                switch (info.orientation)
                {
                    case 270:
                        transpose = "transpose=cclock";
                        break;
                    case 90:
                        transpose = "transpose=clock";
                        break;
                }
            }
            if (transpose != null)
            {
                filters.add(transpose);
            }
            // Crop (only vertically)
            int width = previewHeight;
            int height = width * videoHeight / videoWidth;
            String crop = String.format("crop=%d:%d:%d:%d", width, height, (previewHeight - width) / 2, (previewWidth - height) / 2);
            filters.add(crop);
            // Scale (to designated size)
            String scale = String.format("scale=%d:%d", videoHeight, videoWidth);
            filters.add(scale);

            FFmpegFrameFilter frameFilter = new FFmpegFrameFilter(TextUtils.join(",", filters), previewWidth, previewHeight);
            frameFilter.setPixelFormat(avutil.AV_PIX_FMT_NV21);
            frameFilter.setFrameRate(frameRate);
            try
            {
                frameFilter.start();
            } catch (FrameFilter.Exception e)
            {
                e.printStackTrace();
            }

            isRunning = true;
            RecordedFrame recordedFrame;

            int frameIndex = 0;
            int step = 1;
            final int SAMPLE_LENGTH = 30;
            long[] processFrameTimeSample = new long[SAMPLE_LENGTH];
            int sampleIndex = 0;

            while (isRunning)
            {
                try
                {
                    recordedFrame = mRecordedFrameQueue.take();
                } catch (InterruptedException ie)
                {
                    ie.printStackTrace();
                    try
                    {
                        frameFilter.stop();
                    } catch (FrameFilter.Exception e)
                    {
                        e.printStackTrace();
                    }
                    break;
                }

                mProcessedFrameCount++;
                /* Process only 1st frame in every [step] frames,
                in case the recorded frame queue gets bigger and bigger,
                eventually run out of memory. */
                frameIndex = (frameIndex + 1) % step;
                if (frameIndex == 0)
                {
                    if (mFrameRecorder != null)
                    {
                        long timestamp = recordedFrame.getTimestamp();
                        if (timestamp > mFrameRecorder.getTimestamp())
                        {
                            mFrameRecorder.setTimestamp(timestamp);
                        }
                        long startTime = System.currentTimeMillis();
                        Frame filteredFrame = null;
                        try
                        {
                            frameFilter.push(recordedFrame.getFrame());
                            filteredFrame = frameFilter.pull();
                        } catch (FrameFilter.Exception e)
                        {
                            e.printStackTrace();
                        }
                        try
                        {
                            mFrameRecorder.record(filteredFrame, avutil.AV_PIX_FMT_NV21);
                        } catch (FFmpegFrameRecorder.Exception e)
                        {
                            e.printStackTrace();
                        }
                        long endTime = System.currentTimeMillis();
                        long processTime = endTime - startTime;
                        processFrameTimeSample[sampleIndex] = processTime;
                        mTotalProcessFrameTime += processTime;
                        Log.d(LOG_TAG, "this process time: " + processTime);
                        long totalAvg = mTotalProcessFrameTime / mProcessedFrameCount;
                        Log.d(LOG_TAG, "avg process time: " + totalAvg);
                        // TODO looking for a better way to adjust the process time per frame, hopefully to keep up with the onPreviewFrame callback frequency
                        if (sampleIndex == SAMPLE_LENGTH - 1)
                        {
                            long sampleSum = 0;
                            for (long pft : processFrameTimeSample)
                            {
                                sampleSum += pft;
                            }
                            long sampleAvg = sampleSum / SAMPLE_LENGTH;
                            double tolerance = 0.25;
                            if (sampleAvg > totalAvg * (1 + tolerance))
                            {
                                // ignore more frames
                                step++;
                                Log.i(LOG_TAG, "increase step to " + step);
                            } else if (sampleAvg < totalAvg * (1 - tolerance))
                            {
                                // ignore less frames
                                if (step > 1)
                                {
                                    step--;
                                    Log.i(LOG_TAG, "decrease step to " + step);
                                }
                            }
                        }
                        sampleIndex = (sampleIndex + 1) % SAMPLE_LENGTH;
                    }
                }
                Log.d(LOG_TAG, mProcessedFrameCount + " / " + mRecordedFrameCount);
                mRecycledFrameQueue.offer(recordedFrame);
            }
        }

        public void stopRunning()
        {
            while (getState() != WAITING)
            {
            }
            this.isRunning = false;
            interrupt();
        }
    }

    class FinishRecordingTask extends AsyncTask<Void, Integer, Void>
    {

        ProgressDialog mProgressDialog;

        @Override
        protected Void doInBackground(Void... params)
        {
            //  pauseRecording();
            stopRecording();
            stopRecorder(true);
            releaseRecorder();
            return null;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            mProgressDialog = ProgressDialog.show(FFmpegRecordActivity.this, null, getString(R.string.processing), true);
        }

        @Override
        protected void onPostExecute(Void aVoid)
        {
            super.onPostExecute(aVoid);
            mProgressDialog.dismiss();

//            Intent intent = new Intent(FFmpegRecordActivity.this, PlaybackActivity.class);
//            Bundle mBundle = new Bundle();
//            mBundle.putString(PlaybackActivity.INTENT_NAME_VIDEO_PATH, mVideo.getPath());
//            mBundle.putString(PlaybackActivity.RANDOM_CODE, mStrRandomCode);
//            intent.putExtras(mBundle);
//            startActivity(intent);
            if (isSaved)
            {
                updateRecordVideo(mVideo.getPath(), mStrRandomCode);
            }

        }

        @Override
        protected void onProgressUpdate(Integer... values)
        {
            super.onProgressUpdate(values);
            mProgressDialog.setProgress(values[0]);
        }
    }


    @Override
    protected void onPause()
    {
        super.onPause();
        pauseRecording();
        stopRecording();
        stopPreview();
        bar = 0;
        mPbRecordBar.setProgress(0);
        releaseCamera();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        stopRecorder(false);
        mPbRecordBar.setProgress(0);
        releaseRecorder();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        acquireCamera();
        SurfaceTexture surfaceTexture = mPreview.getSurfaceTexture();
        if (surfaceTexture != null)
        {
            startPreview(surfaceTexture);
            if (mFrameRecorder == null)
            {
                new Thread()
                {
                    @Override
                    public void run()
                    {
                        initRecorder();
                        startRecorder();
                        startRecording();
                    }
                }.start();
            } else
            {
                startRecording();
            }
        }
    }

    private int bar = 0;
    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0:

                    new Handler().postDelayed(new Runnable()
                    {

                        @Override
                        public void run()
                        {
                            bar += 1;
                            runOnUiThread(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    mPbRecordBar.setProgress(bar);
                                }
                            });
                        }
                    }, 1000);
                    break;
            }
        }
    };

    /**
     * 上传视频
     */
    private void updateRecordVideo(String path, String code)
    {
        SVProgressHUD.showWithStatus(FFmpegRecordActivity.this, "加载中...");
        File tempFile = new File(path);
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("video", tempFile);
        params.put("code", code);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.ADDVIDEORECORD, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                Log.d("LoginActivity", response);
                ToastUtils.showToast(FFmpegRecordActivity.this, "视频上传成功");
                SVProgressHUD.dismiss(FFmpegRecordActivity.this);
                finish();
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(FFmpegRecordActivity.this, "视频上传失败");
                SVProgressHUD.dismiss(FFmpegRecordActivity.this);
            }
        });
    }
}
